home *** CD-ROM | disk | FTP | other *** search
/ Atari Mega Archive 1 / Atari Mega Archive - Volume 1.iso / archiver / arc.zoo / arcext.c < prev    next >
C/C++ Source or Header  |  1989-01-29  |  5KB  |  211 lines

  1. /*
  2.  * $Header: arcext.c,v 1.5 88/06/01 19:26:31 hyc Locked $
  3.  */
  4.  
  5. /*
  6.  * ARC - Archive utility - ARCEXT
  7.  * 
  8.  * Version 2.19, created on 10/24/86 at 14:53:32
  9.  * 
  10.  * (C) COPYRIGHT 1985 by System Enhancement Associates; ALL RIGHTS RESERVED
  11.  * 
  12.  * By:  Thom Henderson
  13.  * 
  14.  * Description: This file contains the routines used to extract files from an
  15.  * archive.
  16.  * 
  17.  * Language: Computer Innovations Optimizing C86
  18.  */
  19. #include <stdio.h>
  20. #include "arc.h"
  21. #if    !MSDOS
  22. #include <ctype.h>
  23. #endif
  24.  
  25. void    openarc(), closearc(), setstamp();
  26. #ifdef __STDC__
  27. int    match(), readhdr(), unpack();
  28. #else
  29. int    free(), match(), readhdr(), unpack();
  30. #endif
  31. #ifndef __STDC__
  32. char    *strcpy(), *strcat();
  33. #endif
  34.  
  35. void
  36. extarc(num, arg, prt)        /* extract files from archive */
  37.     int             num;    /* number of arguments */
  38.     char           *arg[];    /* pointers to arguments */
  39.     int             prt;        /* true if printing */
  40. {
  41.     struct heads    hdr;    /* file header */
  42.     int             save;    /* true to save current file */
  43.     int             did[MAXARG];/* true when argument was used */
  44.     char           *i;    /* string index */
  45.     char          **name;    /* name pointer list,
  46.                          * allocator */
  47. #ifndef __STDC__
  48.     char        *malloc(), *index(), *rindex();
  49. #endif
  50.     int             n;    /* index */
  51.     void            extfile();
  52.  
  53.     name = (char **) malloc(num * sizeof(char *));    /* get storage for name
  54.                              * pointers */
  55.  
  56.     for (n = 0; n < num; n++) {    /* for each argument */
  57.         did[n] = 0;    /* reset usage flag */
  58. #if    !MTS
  59.         if (!(i = rindex(arg[n], '\\')))    /* find start of name */
  60.             if (!(i = rindex(arg[n], '/')))
  61.                 if (!(i = rindex(arg[n], ':')))
  62.                     i = arg[n] - 1;
  63. #else
  64.         if (!(i = rindex(arg[n], sepchr[0])))
  65.             if (arg[n][0] != tmpchr[0])
  66.                 i = arg[n] - 1;
  67.             else
  68.                 i = arg[n];
  69. #endif
  70.         name[n] = i + 1;
  71.     }
  72.  
  73.  
  74.     openarc(0);        /* open archive for reading */
  75.  
  76.     if (num) {        /* if files were named */
  77.         while (readhdr(&hdr, arc)) {    /* while more files to check */
  78.             save = 0;    /* reset save flag */
  79.             for (n = 0; n < num; n++) {    /* for each template
  80.                              * given */
  81.                 if (match(hdr.name, name[n])) {
  82.                     save = 1;    /* turn on save flag */
  83.                     did[n] = 1;    /* turn on usage flag */
  84.                     break;    /* stop looking */
  85.                 }
  86.             }
  87.  
  88.             if (save)    /* extract if desired, else skip */
  89.                 extfile(&hdr, arg[n], prt);
  90.             else
  91.                 fseek(arc, hdr.size, 1);
  92.         }
  93.     } else
  94.         while (readhdr(&hdr, arc))    /* else extract all files */
  95.             extfile(&hdr, "", prt);
  96.  
  97.     closearc(0);        /* close archive after reading */
  98.  
  99.     if (note) {
  100.         for (n = 0; n < num; n++) {    /* report unused args */
  101.             if (!did[n]) {
  102.                 printf("File not found: %s\n", arg[n]);
  103.                 nerrs++;
  104.             }
  105.         }
  106.     }
  107.     free(name);
  108. }
  109.  
  110. void
  111. extfile(hdr, path, prt)        /* extract a file */
  112.     struct heads   *hdr;    /* pointer to header data */
  113.     char           *path;    /* pointer to path name */
  114.     int             prt;    /* true if printing */
  115. {
  116.     FILE           *f;        /* extracted file, opener */
  117. #ifndef __STDC__
  118.     FILE        *fopen();
  119. #endif
  120.     char            buf[STRLEN];    /* input buffer */
  121.     char            fix[STRLEN];    /* fixed name buffer */
  122.     char           *i, *rindex();    /* string index */
  123.  
  124.     if (prt) {        /* printing is much easier */
  125.         unpack(arc, stdout, hdr);    /* unpack file from archive */
  126.         printf("\f");    /* eject the form */
  127.         return;        /* see? I told you! */
  128.     }
  129.     strcpy(fix, path);    /* note path name template */
  130. #if    !MTS
  131.     if (*path) {
  132.     if (!(i = rindex(fix, '\\')))    /* find start of name */
  133.         if (!(i = rindex(fix, '/')))
  134.             if (!(i = rindex(fix, ':')))
  135.                 i = fix - 1;
  136.     } else i = fix -1;
  137. #else
  138.     if (!(i = rindex(fix, sepchr[0])))
  139.         if (fix[0] != tmpchr[0])
  140.             i = fix - 1;
  141.         else
  142.             i = fix;
  143. #endif
  144.     strcpy(i + 1, hdr->name);    /* replace template with name */
  145.  
  146.     if (note)
  147.         printf("Extracting file: %s\n", fix);
  148.  
  149.     if (warn && !overlay) {
  150.         if (f = fopen(fix, "r")) {    /* see if it exists */
  151.                 fclose(f);
  152.                 printf("WARNING: File %s already exists!", fix);
  153.                 fflush(stdout);
  154.                 while (1) {
  155.                     printf("  Overwrite it (y/n)? ");
  156.                     fflush(stdout);
  157.                     fgets(buf, STRLEN, stdin);
  158.                     *buf = toupper(*buf);
  159.                     if (*buf == 'Y' || *buf == 'N')
  160.                         break;
  161.                 }
  162.                 if (*buf == 'N') {
  163.                     printf("%s not extracted.\n", fix);
  164.                     fseek(arc, hdr->size, 1);
  165.                     return;
  166.                 }
  167.         }
  168.     }
  169. #if    !MTS
  170.     if (!(f = fopen(fix, OPEN_W)))
  171. #else
  172.     {
  173.         fortran         create();
  174.         void        memset();
  175.         char            c_name[256];
  176.         struct crsize {
  177.             short           maxsize, cursize;
  178.         }               c_size;
  179.         char            c_vol[6];
  180.         int             c_type;
  181.         strcpy(c_name, fix);
  182.         strcat(c_name, " ");
  183.         c_size.maxsize = 0;
  184.         c_size.cursize = hdr->length / 4096 + 1;
  185.         memset(c_vol, 0, sizeof(c_vol));
  186.         c_type = 0x100;
  187.         create(c_name, &c_size, c_vol, &c_type);
  188.     }
  189.     if (image) {
  190.         f = fopen(fix, "wb");
  191.     } else
  192.         f = fopen(fix, "w");
  193.     if (!f)
  194. #endif
  195.     {
  196.         if (warn) {
  197.             printf("Cannot create %s\n", fix);
  198.             nerrs++;
  199.         }
  200.         fseek(arc, hdr->size, 1);
  201.         return;
  202.     }
  203.     /* now unpack the file */
  204.  
  205.     unpack(arc, f, hdr);    /* unpack file from archive */
  206.     fclose(f);        /* all done writing to file */
  207. #if    !MTS
  208.     setstamp(fix, hdr->date, hdr->time);    /* use filename for stamp */
  209. #endif
  210. }
  211.